//
//  PGTagController.m
//  youqu
//
//  Created by 彭鹏 on 16/9/16.
//  Copyright © 2016年 pengpeng. All rights reserved.
//

#import "PGTagController.h"
#import "PGTagCell.h"
#import "PGTagItem.h"
#import "PGHTTPSessionManager.h"

#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <SVProgressHUD/SVProgressHUD.h>


@interface PGTagController ()
/**<#anotation#>*/
@property (nonatomic, strong) NSMutableArray *tags;
/**<#name#>*/
@property (nonatomic, weak) PGHTTPSessionManager *manager;
@end

@implementation PGTagController

- (PGHTTPSessionManager *)manager {
    if (!_manager) {
        PGHTTPSessionManager *manager = [PGHTTPSessionManager manager];
        _manager = manager;
    }
    return _manager;
    
}

static NSString *const tagCellID = @"tag";

- (NSArray *)tags {
    if (_tags == nil) {
        _tags = [NSMutableArray array];
    }
    return _tags;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"推荐标签";
    self.view.backgroundColor = [UIColor grayColor];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PGTagCell class]) bundle:nil] forCellReuseIdentifier:tagCellID];
    
    [SVProgressHUD showWithStatus:@"正在加载..."];
//    [NSThread sleepForTimeInterval:10.0];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
    });
    [self loadData];
    
    
//    self.tableView.separatorInset = UIEdgeInsetsZero;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
 
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
//    [_manager.tasks makeObjectsPerformSelector:@selector(cancel)];
    [_manager invalidateSessionCancelingTasks:YES];
    
}

- (void)loadData {
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    _manager = manager;
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"tag_recommend";
    parameters[@"action"] = @"sub";
    parameters[@"c"] = @"topic";
    NSLog(@"%@",_manager);
    __weak typeof(self) weakSelf = self;
    [self.manager GET:PGBaseUrl parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableArray *tags = [PGTagItem mj_objectArrayWithKeyValuesArray:responseObject];
        weakSelf.tags = tags;
        
        
        [weakSelf.tableView reloadData];
        [SVProgressHUD dismiss];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"网络繁忙,请稍后再试!"];
    }];
    
}




#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.tags.count;
}

#pragma mark - Table view delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PGTagCell *cell = [tableView dequeueReusableCellWithIdentifier:tagCellID forIndexPath:indexPath];
    PGTagItem *tagItem = self.tags[indexPath.row];
    cell.tagItem = tagItem;
    
//    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//        cell.layoutMargins = UIEdgeInsetsZero;
//    }
    

    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

@end
